package com.mini.cn.goods.servlet.ws;

import com.mini.cn.goods.common.observes.YaKashMessageManager;
import com.mini.cn.goods.io.netty.ws.IWsHandel;
import com.mini.cn.goods.io.netty.ws.ParameterMap;
import com.mini.cn.goods.io.netty.ws.WebSocketSession;
import com.mini.cn.goods.io.netty.ws.info.SessionUserInfo;
import com.mini.cn.goods.observer.message.ws.MessageFactory;
import com.mini.cn.goods.observer.message.ws.SocketOpenMessage;
import com.mini.cn.goods.servlet.ws.common.PushHelper;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: fly
 * Date: 2022/12/11
 * Time: 23:39
 */
@Slf4j
public class PushWsHandle implements IWsHandel {

    @Override
    public void onHandShakeFail(WebSocketSession session) {

    }

    @Override
    public void onOpen(WebSocketSession session, HttpHeaders headers, ParameterMap parameterMap) throws IOException {
        //总连接数
        WSApplicationData.INSTANCE.getActiveSession().incrementAndGet();
        //发送open消息
        SocketOpenMessage openMessage = MessageFactory.createSocketOpenMsg(session, parameterMap);
        YaKashMessageManager.INSTANCE.productMessage(openMessage);
    }

    @Override
    public void onBinary(WebSocketSession session, byte[] bytes) {
        if (getLegalConnection(session) == null) {
            return;
        }
    }

    @Override
    public void onEvent(WebSocketSession session, Object evt) {
        SessionUserInfo userInfo = getLegalConnection(session);
        if (userInfo == null) {
            return;
        }
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
            switch (idleStateEvent.state()) {
                case READER_IDLE:
                    //到了read 的时间
                    log.info("READER_IDLE come in");
                    session.close();
                    break;
                case WRITER_IDLE:
                    log.info("WRITER_IDLE come in");
                    break;
                case ALL_IDLE:
                    log.info("ALL_IDLE come in");
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onMessage(WebSocketSession session, String message) {
        if (getLegalConnection(session) == null) {
            return;
        }
    }

    @Override
    public void onPingMessage(WebSocketSession session) {

    }

    @Override
    public void onClose(WebSocketSession session) throws IOException {
        removeSession(session);
    }

    private void removeSession(WebSocketSession session) {
        if (session != null) {
            String frontApp = "";
            SessionUserInfo userInfo = session.getAttribute(WebSocketSession.USERKEY);
            if (userInfo != null) {
                String userid = userInfo.getUserId();
                log.info("user:" + userid + " close  ....");
                frontApp = userInfo.getFrontApp();
                WSApplicationData.INSTANCE.getUserSessionMap().remove(userid);
                //清理注册的缓存信息
                PushHelper.clearRegisterCache(session, userInfo);
            }
            WSApplicationData.INSTANCE.getActiveSession().decrementAndGet();
            //总断连数
        }
    }

    @Override
    public void onError(WebSocketSession session, Throwable throwable) {

    }

    /**
     * 判断是否是正常注册的连接
     *
     * @param socketSession
     * @return
     */
    private SessionUserInfo getLegalConnection(WebSocketSession socketSession) {
        SessionUserInfo userInfo = socketSession.getAttribute(WebSocketSession.USERKEY);
        if (userInfo == null) {
            socketSession.close();
            return null;
        }
        return userInfo;
    }
}
